package com.kll.tv.views.fragment

import android.graphics.Bitmap
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AnimationUtils
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.kll.tv.R
import com.kll.tv.views.ui.PinchImageView

/**
 * 图片预览Fragment
 */
class ImageReviewFragment: Fragment() {
    private lateinit var pinchImageView: PinchImageView
    private lateinit var requestOptions: RequestOptions

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_image_review, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        pinchImageView = requireView().findViewById(R.id.pv_pinchImageView)

        val imageUrl = arguments?.get("imageUrl")

        requestOptions = RequestOptions().placeholder(resources.getDrawable(R.drawable.transparent, null))

//        Glide.with(this).load(imageUrl).apply(requestOptions!!).into(pinchImageView)

        Glide.with(this).asBitmap().load(imageUrl).into(object: SimpleTarget<Bitmap>(){
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                pinchImageView.setImageBitmap(resource)
            }

        })

        pinchImageView.reset()

        val animation = AnimationUtils.loadAnimation(context, R.anim.view_anim)
        pinchImageView.startAnimation(animation)


        // 点击操作为退出
        pinchImageView.setOnClickListener { activity?.finish() }
    }

}